package com.wz.activity.main.helper;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

//import com.xlh.Utils.DatabaseHelper;
import com.xlh.Model.ButCmdsModel;
import com.xlh.bean.DataBase.ButCmd;
import com.xlh.outside.MainActivity;
import com.xlh.outside.R;

import java.util.ArrayList;
import java.util.List;

public class MainInputBox {

	private MainActivity activity;
	private String buKey;
	private List<ButCmd> butCmdsList = new ArrayList<ButCmd>();

	public String getParentButKey() {
		return parentButKey;
	}

	public MainInputBox setParentButKey(String parentButKey) {
		this.parentButKey = parentButKey;
		return this;
	}

	private String parentButKey;

	public String getBuKey() {
		return buKey;
	}

	public MainInputBox setBuKey(String buKey) {
		this.buKey = buKey;
		return this;
	}

	public MainInputBox(Context context){
		this.activity = (MainActivity)context;
	}



	public void run(){

		LayoutInflater inflater = activity.getLayoutInflater();
		View layout = inflater.inflate(R.layout.max_edit,null);

		butCmdsList = activity.getDiyButs();


		final EditText maxEdit = (EditText)layout.findViewById(R.id.edt_max_edit);
		Dialog alertDialog = new AlertDialog.Builder(activity)
				.setTitle("设置自定义按钮")
				.setView(layout)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {

							ButCmdsModel butCmdsModel = new ButCmdsModel(activity);
							ButCmd data = butCmdsModel.getByParentKeyAndButKey(getParentButKey(),getBuKey());
							if( data==null ){
								//不存在记录 添加
								data = new ButCmd();
								data.setKey(getBuKey());
								data.setParentKey(getParentButKey());
								data.setCmds(maxEdit.getText().toString());
								butCmdsModel.add(data);

							}else{
								//有数据则更新数据
								data = new ButCmd();
								data.setKey(getBuKey());
								data.setParentKey(getParentButKey());
								data.setCmds(maxEdit.getText().toString());
								butCmdsModel.update(data);

							}

							//更新自定义按键集合数据
							activity.setDiyButs(butCmdsModel.getList());
							//activity.sendActionToServer("mycmds");

							Toast tst = Toast.makeText(activity, "设置成功！切换后生效！", Toast.LENGTH_SHORT);
							tst.setGravity(Gravity.BOTTOM, 0, 30);
							tst.show();



						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}})

				.setNeutralButton("恢复默认",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						ButCmdsModel butCmdsModel = new ButCmdsModel(activity);
						if(butCmdsModel==null) {
							Toast tst = Toast.makeText(activity, "自定义按键数据库为空！", Toast.LENGTH_SHORT);
							tst.setGravity(Gravity.BOTTOM, 0, 30);
							tst.show();
							return;

						}
						ButCmd data = new ButCmd();
						data.setParentKey(getParentButKey());
						data.setKey(getBuKey());
						butCmdsModel.delete(data);
						activity.setDiyButs(butCmdsModel.getList());
						Toast tst = Toast.makeText(activity, "已恢复默认按钮！切换后生效！", Toast.LENGTH_SHORT);
						tst.setGravity(Gravity.BOTTOM, 0, 30);
						tst.show();


					}
				})


				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
								dialog.dismiss();
					}

				}).create();

				//填充数据
				//for(ButCmd btCmd :butCmdsList ){
				if(butCmdsList!=null){
					for(int i=0;i<butCmdsList.size();i++ ){
						if(butCmdsList.get(i).getKey().equals(getBuKey()) && butCmdsList.get(i).getParentKey().equals(getParentButKey())){
							maxEdit.setText(butCmdsList.get(i).getCmds());
							break;
						}
					}
				}


				alertDialog.show();

	}




}
